from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis.serializers import json
from rest_framework import request
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from xj_users.models import User, Article


#获取用户数据
class UserListView(View):

    # 获取用户数据
    # def get(self, request):
    #
    #     #1.查询模型，获取查询集
    #     users = User.objects.all()
    #     #2.遍历查询集，将对象列表转换为字典
    #     user_list = []
    #     for people in users:
    #         user_list.append({
    #             'id':people.id,
    #             'name':people.name,
    #             'sex':people.sex,
    #             'pub_date':people.pub_date,
    #             'webist':people.website,
    #             'mobile':people.mobile,
    #             'email':people.email,
    #             'city':people.city,
    #             'address':people.address,
    #         })
    #     #user_list 是字典要想不报错，加safe = False
    #     return  JsonResponse(user_list, safe=False)


    # 修改用户数据
    def put(self, request, pk):
        # 1.根据id查询数据
        try:
            user = User.objects.get(id=pk)
        except User.DoesNotExist:
            return JsonResponse({'msg': '没有此数据'})

        # 2.接受修改的数据
        data = json.loads(request.body.decode())
        # 3.验证新数据
        # 4.调用模型的更新方法，更新数据入库
        user.realname = data.get('name', user.realname)
        user.sex = data.get('sex', user.sex)
        user.birthday = data.get('pub_date', user.birthday)
        user.website = data.get('website', user.website)
        user.mobile = data.get('mobile', user.mobile)
        user.email = data.get('email', user.email)
        user.city = data.get('city', user.city)
        user.address = data.get('address', user.address)
        user.avatar = data.get('avatar', user.avatar)
        #
        # 调用save（）
        user.save()
        # 5.返回相应
        user_dict = {
            'id': user.id,
            'realname': user.realname,
            'sex':user.sex,
            'pub_date': user.pub_date,
            'website':user.website,
            'mobile':user.mobile,
            'email':user.email,
            'city':user.city,
            'address':user.address,
            'avatar':user.avatar
        }

        return JsonResponse(user_dict)


#关注用户
class AttenttionUserView(APIView):
    # permission_classes = [IsAuthenticated]
    #关注用户
    def post(self, request, id):
        user = request.user
        try:
            yonghu = User.objects.get(id=id)
        except User.DoesNotExist:
            return Response({'message': "关注失败",
                             'success': 'false'})
        user.idols.add(yonghu)
        user.save()
        # self.request.user.fans.add(yonghu)
        return Response({'message': "关注成功",
                         'success': 'true'})
    #取消关注用户
    def delete(self, request, id):

        user = request.user
        try:
            yonghu = User.objects.get(id=id)
        except User.DoesNotExist:
            return Response({'message': "取消关注失败",
                             'success': 'false'})

        user.idols.remove(yonghu)
        user.save()
        # self.request.user.fans.remove(yonghu)
        return Response({"message": "取消关注成功",
                         "success": "true"})








